package org.jeecg.modules.demo.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.crm.entity.CCommon;
import org.jeecg.modules.demo.crm.mapper.CCommonMapper;
import org.jeecg.modules.demo.crm.service.ICCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 通用方法
 * @Author: jeecg-boot
 * @Date:   2025-06-24
 * @Version: V1.0
 */
@Service
public class CCommonServiceImpl extends ServiceImpl<CCommonMapper, CCommon> implements ICCommonService {


    @Autowired
    public CCommonMapper cCommonMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public String translatData(String columnName,String tableName,String id) {
        return cCommonMapper.translatData(columnName, tableName, id);
    }

    /**
     * 权限赋值
     * @param queryWrapper
     * @return
     */
    @Override
    public QueryWrapper permissionDetection(QueryWrapper queryWrapper) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(!"admin".equals(sysUser.getRoleCode()) &&
                !"项目经理".equals(sysUser.getRoleCode()) &&
                !"项目负责人".equals(sysUser.getRoleCode())
                && !"财务出纳".equals(sysUser.getRoleCode()) ){
            queryWrapper.eq("user_id", sysUser.getId());


        }
        return queryWrapper;
    }

    @Override
    public QueryWrapper permissionDetectionColumn(QueryWrapper queryWrapper, String columnName) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(!"admin".equals(sysUser.getRoleCode()) &&
                !"项目经理".equals(sysUser.getRoleCode()) &&
                !"项目负责人".equals(sysUser.getRoleCode())
                && !"财务出纳".equals(sysUser.getRoleCode()) ){
            queryWrapper.eq(columnName, sysUser.getId());

        }
        return queryWrapper;
    }

    @Override
    public QueryWrapper permissionDetectionCreateBy(QueryWrapper queryWrapper) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(!"admin".equals(sysUser.getRoleCode()) &&
                !"项目经理".equals(sysUser.getRoleCode())
                && !"项目负责人".equals(sysUser.getRoleCode())
                && !"财务出纳".equals(sysUser.getRoleCode()) ){
            queryWrapper.eq("create_by", sysUser.getUsername());
        }
        return queryWrapper;
    }



    @Override
    public String executeQuery(String sql) {
        try {
            // 使用JdbcTemplate查询单个结果
            String result = jdbcTemplate.queryForObject(sql, String.class);
            return result;
        } catch (Exception e) {
            // 处理异常，例如SQL语法错误等
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Boolean permissionCheck() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(!"admin".equals(sysUser.getRoleCode()) &&
                !"项目经理".equals(sysUser.getRoleCode())
                && !"项目负责人".equals(sysUser.getRoleCode())
                && !"财务出纳".equals(sysUser.getRoleCode())
                && !sysUser.getRoleCode().contains("维护") ){
            //不是公司主要用户
           return true;
        }
        return false;
    }

    @Override
    public Boolean taskpermissionCheck() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(!"admin".equals(sysUser.getRoleCode()) &&
                !"项目经理".equals(sysUser.getRoleCode())
                && !"项目负责人".equals(sysUser.getRoleCode())
                && !"财务出纳".equals(sysUser.getRoleCode())
                && !"scene".equals(sysUser.getRoleCode())){//华瑭
            //不是公司主要用户
            return true;
        }
        return false;
    }


}
